package com.taiyi.renaissancemall.ui.main.order;

import android.content.Intent;
import android.util.Log;

import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.CommitOrderBean;
import com.taiyi.renaissancemall.bean.FreightBean;
import com.taiyi.renaissancemall.bean.PaySuccessBean;
import com.taiyi.renaissancemall.bean.ProductFillOrderBean;
import com.taiyi.renaissancemall.bean.ToFillOrderBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;
import com.taiyi.renaissancemall.ui.login.LoginActivity;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;

public class FillOrderPresent extends BasePresent<FillOrderView> {

    public void getOrderInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("shopcarids", view.getShopCarId());
        HttpUtils.postRequest(BaseUrl.GET_ORDER_INFO, this, map, new JsonCallback<BaseResultBean<ToFillOrderBean>>() {
            @Override
            public BaseResultBean<ToFillOrderBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<ToFillOrderBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getOrderInfoSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getOrderInfoFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<ToFillOrderBean>> response) {
                super.onError(response);
            }
        });
    }


    public void getProductOrderInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("commodityspecid", view.getShopCarId());
        map.put("num", view.getNum());

        HttpUtils.postRequest(BaseUrl.PRODUCT_GET_ORDER_INFO, this, map, new JsonCallback<BaseResultBean<ProductFillOrderBean>>() {
            @Override
            public BaseResultBean<ProductFillOrderBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<ProductFillOrderBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getProductOrderInfoSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getProductOrderInfoFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<ProductFillOrderBean>> response) {
                super.onError(response);
            }
        });
    }


    public void commitOrder() {
        Map<String, String> map = new HashMap<>();
        map.put("shopcarids", view.getShopCarId());
        map.put("paytype", view.getPayType());
        map.put("addressid", view.getAddressId());
        map.put("remark", view.getRemark());
        Log.i("ssssddddd", "-----" + view.getAddressId());
        HttpUtils.postRequest(BaseUrl.SHOPPING_CAR_COMMIT_ORDER, this, map, new JsonCallback<BaseResultBean<CommitOrderBean>>() {
            @Override
            public BaseResultBean<CommitOrderBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<CommitOrderBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    ToastUtils.showLongToast(response.body().msg_cn);
                    view.commitOrderSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.commitOrderFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<CommitOrderBean>> response) {
                super.onError(response);
            }
        });

    }

    public void productCommitOrder() {
        Map<String, String> map = new HashMap<>();
        map.put("commodityspecid", view.getShopCarId());
        map.put("paytype", view.getPayType());
        map.put("number", view.getNum());
        map.put("addressid", view.getAddressId());
        map.put("remark", view.getRemark());
        Log.i("ssssddddd", "-----" + view.getAddressId());
        HttpUtils.postRequest(BaseUrl.PRODUCT_COMMIT_ORDER, this, map, new JsonCallback<BaseResultBean<CommitOrderBean>>() {
            @Override
            public BaseResultBean<CommitOrderBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<CommitOrderBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
//                    ToastUtils.showLongToast(response.body().msg_cn);
                    view.commitOrderSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.commitOrderFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<CommitOrderBean>> response) {
                super.onError(response);
            }
        });

    }


    public void findOrderState() {
        Map<String, String> map = new HashMap<>();
        map.put("orderid", view.getOrderId());
        Log.i("sssssss", "------" + view.getOrderId());
        HttpUtils.postRequest(BaseUrl.FIND_ORDER_STATE, this, map, new JsonCallback<BaseResultBean<PaySuccessBean>>() {
            @Override
            public BaseResultBean<PaySuccessBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<PaySuccessBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    String ordertype = response.body().data.getOrderzhu().getOrdertype();//1成功
                    if ("1".equals(ordertype)) {
                        view.getOrderStateSuccess();
                    } else {
                        findOrderState();
                    }
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    findOrderState();
                    view.getOrderStateFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<PaySuccessBean>> response) {
                super.onError(response);
            }
        });
    }


    public void getFreight() {
        Map<String, String> map = new HashMap<>();
        map.put("addressid", view.getAddressId());
        map.put("commodityspecids", view.getCommSpecId());
        HttpUtils.postRequest(BaseUrl.GET_FREIGHT, this, map, new JsonCallback<BaseResultBean<FreightBean>>() {

            @Override
            public BaseResultBean<FreightBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<FreightBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getFreightSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getFreightFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<FreightBean>> response) {
                super.onError(response);
            }

        });
    }

}
